#library(SmarterPoland)
###Collecting eurostat country information
##GDP
gdp <- getEurostatRCV(kod = "nama_aux_gph")
gdp1<-subset(gdp, unit=='PPS_HAB' & as.numeric(as.character(time))=='2005')
gdp1<-subset(gdp1, geo=='AT'| geo=='BG'| geo=='CY'
             | geo=='CZ'| geo=='DE'| geo=='DK'| geo=='EE'| geo=='EL'
             | geo=='ES'| geo=='FI'| geo=='FR'| geo=='HU'
             | geo=='IE'| geo=='IT'| geo=='LT'| geo=='LU'
             | geo=='LV'| geo=='NL'| geo=='PL'| geo=='PT'
             | geo=='RO'| geo=='SI'| geo=='SK'| geo=='UK')
#Recode the countries to match
gdp1$geo<-recode(gdp1$geo, '"UK"="GB"; "EL"="GR"')
colnames(gdp1)[5]<-"gdp"
gdp1<-gdp1[,c(3,5)]

##Main religion (dummy)
gdp1$rel.camp<-c("cat","cat","cat","cat","prot","prot", "prot","cat","cat","prot","cat","cat","cat","cat","cat",
                 "cat","prot","prot","cat","cat","cat","cat","cat","prot") #N.B. it is a dummy protestant

###GDP growth
gdp.gr <- getEurostatRCV(kod = "nama_gdp_k")
gdp.gr1<-subset(gdp.gr, unit=='PCH_PRE' & indic_na=='B1GM' & as.numeric(as.character(time))=='2005')
gdp.gr1<-subset(gdp.gr1, geo=='AT'| geo=='BG'| geo=='CY'
             | geo=='CZ'| geo=='DE'| geo=='DK'| geo=='EE'| geo=='EL'
             | geo=='ES'| geo=='FI'| geo=='FR'| geo=='HU'
             | geo=='IE'| geo=='IT'| geo=='LT'| geo=='LU'
             | geo=='LV'| geo=='NL'| geo=='PL'| geo=='PT'
             | geo=='RO'| geo=='SI'| geo=='SK'| geo=='UK')
#Recode the countries to match
gdp.gr1$geo<-recode(gdp.gr1$geo, '"UK"="GB"; "EL"="GR"')
gdp.gr1<-gdp.gr1[,c(3,5)]
colnames(gdp.gr1)[2]<-"gdp.gr"

##Percentage spen on social secutiry
soc<-read.csv("E:/gov_a_exp_1_Data.csv")
#soc <- getEurostatRCV(kod = "gov_a_exp") #doesnt work for some reason
soc1<-soc[,c(2,7)]
colnames(soc1)<-c('geo', 'soc')
soc1<-subset(soc1, geo=='AT'| geo=='BG'| geo=='CY'
             | geo=='CZ'| geo=='DE'| geo=='DK'| geo=='EE'| geo=='EL'
             | geo=='ES'| geo=='FI'| geo=='FR'| geo=='HU'
             | geo=='IE'| geo=='IT'| geo=='LT'| geo=='LU'
             | geo=='LV'| geo=='NL'| geo=='PL'| geo=='PT'
             | geo=='RO'| geo=='SI'| geo=='SK'| geo=='UK')
#Recode the countries to match
soc1$geo<-recode(soc1$geo, '"UK"="GB"; "EL"="GR"')

##government effectivness (2006)
gov<-read.table("E:/goveff.txt", header=T, sep='\t', dec=",")

##immigartion as percentage from total population (2006 for citizenship; 2009 for country of birth)
im<-read.table("E:/eurostat_im.txt", header=T, sep='\t', dec=",")


allvars<-merge(gdp1, soc1,by="geo")
allvars<-merge(allvars, gdp.gr1,by="geo")
allvars<-merge(allvars, im,by="geo")
allvars<-merge(allvars, gov,by.x="geo", by.y="wbcode")
allvars$reg<-recode(as.factor(allvars$geo), 'c("AT","BE","DE","DK","GR","ES","FI","FR","IE","IT","NL","PT","SE","GB","LU")=0; else=1')
allvars$ort<-as.factor(recode(as.factor(allvars$geo), 'c("BG","RO","CY","GR")=1; else=0'))
allvars$com<-recode(as.factor(allvars$geo), 'c("AT","CY","MT", "BE","DE","DK","GR","ES","FI","FR","IE","IT","NL","PT","SE","GB","LU")=0; else=1')
allvars$prot<-as.factor(recode(as.factor(allvars$geo), 'c("DE","DK","EE","FI", "GB", "LV","NL")=1; else=0'))

#scale the numerical variables
allvars$gdp<-scale(allvars$gdp)
allvars$soc<-scale(allvars$soc)
allvars$gdp.gr<-scale(allvars$gdp.gr)
allvars$unempl<-scale(allvars$unempl)

allvars
write.table(allvars, "E:/allvars.txt")